<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    private $_id;

    public function authenticate() {
        $user = Users::model()->find('email=:e', array(
            'e' => $this->username,
                ));
		
        if ($user == NULL) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if (md5($this->password) != $user->password) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $user->id;

            $this->errorCode = self::ERROR_NONE;
            Logintime::model()->createEntry($user->id);
            switch ($user->userAccess->type) {
                case Useraccess::Type_Sales_Team :
                    $this->setState('usertype', Useraccess::Type_Sales_Team);
                    $this->setState('empno', $user->employeeid);
                    Yii::app()->user->returnUrl = 'index.php?r=salespersonportal';

                    break;
                case Useraccess::Type_Employee :
                    $this->setState('usertype', Useraccess::Type_Employee);
                    $this->setState('empno', $user->employeeid);
                    Yii::app()->user->returnUrl = 'index.php?r=employeeportal';
                    break;
                case Useraccess::Type_Customer:
                    $this->setState('usertype', Useraccess::Type_Customer);
                    $this->setState('custno', $user->custno);
                    Yii::app()->user->returnUrl = 'index.php?r=customerportal';
                    break;
                case Useraccess::Type_Admin :
                  $this->setState('usertype', Useraccess::Type_Admin);
                    $this->setState('privilege', unserialize($user->userAccess->privilege));
                    //Yii::app()->user->returnUrl = 'index.php?r=site';
                    break;
                    
                    
                default:
                    break;
            }
            /*
              if (!empty($user->custno)) {
              $this->setState('usertype', 'customer');
              $this->setState('custno', $user->custno);
              Yii::app()->user->returnUrl = 'index.php?r=customerportal';
              }
              if (!empty($user->employeeid)) {
              $employee = Employees::model()->findByPk($user->employeeid);

              if ($employee->dept == 'Sales') {
              if ($employee->empno != '1109') {
              $this->setState('usertype', 'salesperson');
              $this->setState('empno', $employee->empno);
              Yii::app()->user->returnUrl = 'index.php?r=salespersonportal';
              }
              } else {

              $this->setState('usertype', 'employee');
              $this->setState('empno', $employee->empno);

              Yii::app()->user->returnUrl = 'index.php?r=employeeportal';
              }
              }
             */
        }

        return!$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

}
